<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1.0, maximum-scale=1.0, minimal-ui" />
        
        <title>Authentication API Documentation</title>
        
        <script src="/js/greyspots.js" type="text/javascript"></script>
        <link href="/css/greyspots.css" type="text/css" rel="stylesheet" />
        
        <script src="/js/ace/ace.js" data-ace-base="/js/ace/" type="text/javascript" charset="utf-8"></script>
        <script src="/js/ace/ext-language_tools.js" type="text/javascript"></script>
        <script src="/js/ace/ext-searchbox.js" type="text/javascript"></script>
        
        <script src="doc-library/doc.js" type="text/javascript"></script>
        <link href="doc-library/doc.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
        <gs-jumbo>
            <h1 class="text-center">Authentication</h1>
        </gs-jumbo>
        <gs-container min-width="sml;med;lrg">
            <h3>URLs:</h3>
            <pre><code>/env/auth
    action=login
    username=<b>&lt;USERNAME&gt;</b>
    password=<b>&lt;PASSWORD&gt;</b>
/env/auth
    action=change_pw
    password_old=<b>&lt;PASSWORD&gt;</b>
    password_new=<b>&lt;NEW-PASSWORD&gt;</b>
/env/auth
    action=logout
/env/action_info</code></pre>
            
            <h3>Description:</h3>
            <p>These links handle Envelope authentication, including login, change password, and logout.</p>
            
            <h1>Examples:</h1>
            <div class="doc-example-description">
                <span class="h3">Login Example:</span><br/>
                <p>Before you can use the envelope website, you need to authenticate yourself to the server. The response from the login ajax call will contain your envelope cookie. All cookies have an expiration date and time determined by the envelope config file. The default is eight hours. This prevents someone from using a stolen cookie forever. The ajax call also returns the home page.</p>
            </div>
            <gs-doc-example>
                <template for="html" height="5">
                    <gs-button onclick="ajax_example()">Login</gs-button>
                </template>
                <template for="js" height="25">
                    function ajax_example() {
                        GS.addLoader('loader', 'Ajax running...');
                        GS.ajaxJSON('/env/auth', 'action=login' +
                            '&username=' + encodeURIComponent('test') +
                            '&password=' + encodeURIComponent('test'), function (data, error) {
                            GS.removeLoader('loader');
                            
                            if (!error) {
                                GS.setCookie('greyspots_uname', document.getElementById('uname').value, 1000000);
                                //window.location = data.dat + '?cache=no';
                                
                            } else {
                                GS.ajaxErrorDialog(data);
                            }
                        });
                    }
                </template>
            </gs-doc-example>
            
            <div class="doc-example-description">
                <span class="h3">Change Password Example:</span><br/>
                <p>Every once in a while, you will want to change your password. This ajax call both changes the password and replaces the envelope cookie.</p>
            </div>
            <gs-doc-example>
                <template for="html" height="5">
                    <gs-button onclick="ajax_example()">Change Password</gs-button>
                </template>
                <template for="js" height="25">
                    function ajax_example() {
                        GS.addLoader('loader', 'Ajax running...');
                        GS.ajaxJSON('/env/auth', 'action=change_pw' +
                            '&password_old=' + encodeURIComponent('test') +
                            '&password_new=' + encodeURIComponent('test'), function (data, error) {
                            GS.removeLoader('loader');
                            
                            if (!error) {
                                
                                
                            } else {
                                GS.ajaxErrorDialog(data);
                            }
                        });
                    }
                </template>
            </gs-doc-example>
            
            <div class="doc-example-description">
                <span class="h3">Logout Example:</span><br/>
                <p>Once you are done working for the day, logout. Your envelope cookie will be destroyed as a result of this ajax call. Without a cookie further calls to the system will fail.</p>
            </div>
            <gs-doc-example>
                <template for="html" height="5">
                    <gs-button onclick="ajax_example()">Logout</gs-button>
                </template>
                <template for="js" height="25">
                    function ajax_example() {
                        GS.addLoader('loader', 'Ajax running...');
                        GS.ajaxJSON('/env/auth', 'action=logout', function (data, error) {
                            GS.removeLoader('loader');
                            
                            if (!error) {
                                
                                
                            } else {
                                GS.ajaxErrorDialog(data);
                            }
                        });
                    }
                </template>
            </gs-doc-example>
            
            <div class="doc-example-description">
                <span class="h3">Info Example:</span><br/>
                <p>Returns Username, Groups, envelope version, cluster version, cluster port number and dbname.</p>
            </div>
            <gs-doc-example>
                <template for="html" height="5">
                    <gs-button onclick="ajax_example()">Info</gs-button>
                    <pre id="ajax_result"></pre>
                </template>
                <template for="js" height="25">
                    function ajax_example() {
                        GS.addLoader('loader', 'Ajax running...');
                        GS.ajaxJSON('/env/action_info', '', function (data, error) {
                            GS.removeLoader('loader');
                            
                            if (!error) {
                                document.getElementById('ajax_result').textContent = JSON.stringify(data.dat);
                            } else {
                                GS.ajaxErrorDialog(data);
                            }
                        });
                    }
                </template>
            </gs-doc-example>
        </gs-container>
    </body>
</html>